package com.wise.service.cms.content.uploadresource;

import com.wise.entities.content.Resource;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.logger.Logger;
import com.wise.framework.session.CurrentUserId;
import com.wise.summer.v1.cms.content.uploadresource.AddUploadResource;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * @author yubo
 */
@Service
@SummerService
public class AddUploadResourceService implements SummerServiceBean<AddUploadResource> {
    private static final Logger log = Logger.getLogger(AddUploadResourceService.class);

    @Override
    public void sum(AddUploadResource summer) {
        String userId = CurrentUserId.s(true);

        Resource resource = new Resource();

        resource.setGroupId(summer.getGroupId());
        resource.setResourceName(summer.getResourceName());
        resource.setPath(summer.getPath());
        resource.setOssId(summer.getOssId());
        resource.setExtentionName(summer.getExtentionName());
        resource.setResourceType(summer.getResourceType());
        resource.setResourcePath(summer.getResourcePath());
        resource.setOriginResourceName(summer.getOriginResourceName());
        resource.setFileSize(summer.getFileSize().intValue());
        resource.setDescribe(summer.getDescribe());
        resource.setLang(summer.getLang());

        Timestamp timestampNow = DateTimeUtil.timestampNow();
        resource.setCreationDate(timestampNow);
        resource.setLastUpdateDate(timestampNow);
        resource.setCreatedBy(userId);
        resource.setLastUpdateBy(userId);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        resource.setUploadTime(format.format(timestampNow));

        DaoSave.s(resource);

        summer.setSummerResult(true);
    }
}
